package service.Impl;

import com.alibaba.fastjson.JSONObject;
import entity.Gradetable;
import entity.Kclr;

import mapper.KclrDao;
import entity.TeacherTable;
import org.springframework.stereotype.Service;
import service.KclrService;
import utils.PageObject;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service("KclrService")
public class KclrServiceImpl implements KclrService {

    @Resource
    private KclrDao kclrdao;

    @Override
    public JSONObject insert(JSONObject insertobj) {
        JSONObject result = new JSONObject();
        Kclr kclr = new Kclr();
        kclr.setZy(insertobj.getString("zy"));
        kclr.setNj(insertobj.getString("nj"));
        kclr.setKskm(insertobj.getString("kskm"));
        kclr.setZsrs(insertobj.getString("zsrs"));
        kclr.setSjys(insertobj.getString("sjys"));
        kclr.setRkls1(insertobj.getString("rkls1"));
        kclr.setRkls2(insertobj.getString("rkls2"));
        kclr.setBz(insertobj.getString("bz"));

        kclr.setId(insertobj.getInteger("id"));


        if (kclr.getId() == null || kclr.getId().equals(0)) {


            int insert = this.kclrdao.insert(kclr);
            if (insert == 1) {
                result.put("code", "0");
                result.put("msg", "数据插入成功!");
            } else {
                result.put("code", "-1");
                result.put("msg", "数据插入失败,请核对!");
            }
        } else {
            int update =   this.kclrdao.update(kclr);
            if (update == 1) {
                result.put("code", "0");
                result.put("msg", "数据修改成功!");
            } else {
                result.put("code", "-1");
                result.put("msg", "数据修改失败,请核对!");
            }

        }
        return result;
    }

    @Override
    public Kclr update(Kclr kclr) {
        return null;
    }

    @Override
    public boolean deleteById(Integer Id) {
        return this.kclrdao.deleteById(Id) > 0;
    }

    @Override
    public PageObject queryAllByLimit(JSONObject limitobj) {

        PageObject pageObject = new PageObject();
        //当前第几页
        Integer page = limitobj.getInteger("page");
        //每页几条
        Integer limit = limitobj.getInteger("limit");
        //计算开始位置
        Integer startindex = (page - 1) * limit;
        List<Kclr> curriculumLists = kclrdao.queryAllByLimit(startindex, limit);
        //查询所有数据条数
        Integer selectcount = kclrdao.selectcount();
        pageObject.setPageCount(page);//第几页
        pageObject.setPageSize(limit);//每页几条
        pageObject.setRowCount(selectcount);//总条数
        pageObject.setPageCurrent((selectcount - 1) / page + 1);//总页数
        pageObject.setRecords(Collections.singletonList(curriculumLists));//当前需展示数据
        return pageObject;
    }
}
